import request from '@/utils/request'

// 获取购买量数据
export function getBuyList(params) {
    return new Promise(async (resolve, reject) => {
        const { data: res } = await request.get('/course/courseBuy', { params })
        if (res.code == 200) {
            let result = {

                xlist: [],
                ylist: [],
                url: '/buy',
                responseList: res.result,
                name: '课程购买量',
                tipname: '购买量',
                type:'line',
                fn:'getBuyList'
            }
            res.result.forEach(item => {
                result.xlist.push(item.day.slice(5));
                result.ylist.push(item.buyNum);
            });
            resolve(result)
        } else {
            reject()
        }
    })
}

// 获取访问量数据
export function getVisiList(params) {
    return new Promise(async (resolve, reject) => {
        const { data: res } = await request.get('/course/courseVisit', { params })
        if (res.code == 200) {
            let result = {
                xlist: [],
                ylist: [],
                url: '/buy?active=visits',
                responseList: res.result,
                name: '课程访问量',
                tipname: '访问量',
                type:'line',
                fn:'getVisiList'
            }
            res.result.forEach(item => {
                result.xlist.push(item.day.slice(5));
                result.ylist.push(item.visitNum);
            });
            resolve(result)
        } else {
            reject()
        }
    })
}

// 获取学习频次
export function getFreData(params) {
    return new Promise(async (resolve, reject) => {
        const { data: res } = await request.get('/course/courseFrequencyDistribution', { params })
        if (res.code == 200) {
            let result = {
                xlist: [],
                ylist: [],
                responseList: res.result,
                name: '学习频率统计',
                tipname: '频率',
                type:'line',
                fn:'getFreData'
            }
            res.result.forEach(item => {
                result.xlist.push(item.frequency);
                result.ylist.push(item.distributionNum);
            });
            resolve(result)
        } else {
            reject()
        }
    })
}

// 获取在线人数
export function getOnlineData(params) {

    return new Promise(async (resolve, reject) => {
        const { data: res } = await request.get("/course/courseOnline", {
            params
        });
        if (res.code == 200) {
            let result = {
                xlist: [],
                ylist: [],
                url: '/online',
                responseList: res.result,
                name: '在线学生人数',
                tipname: '在线人数',
                type:'bar',
                fn:'getOnlineData'
            }
            res.result.forEach(item => {
                result.xlist.push(item.day.slice(5));
                result.ylist.push(item.onlineNum);
            });
            resolve(result)
        } else {
            reject()
        }
    })

}

// 新增用户人数
export function getNewuserData(params) {
    return new Promise(async (resolve, reject) => {
        const { data: res } = await request.get("/user/newUser", {
            params
        });
        if (res.code == 200) {
            let result = {
                xlist: [],
                ylist: [],
                url: '/newuser',
                responseList: res.result,
                name: '新增用户人数',
                tipname: '新增用户',
                type:'line',
                fn:'getNewuserData'
            }
            res.result.forEach(item => {
                result.xlist.push(item.day.slice(5));
                result.ylist.push(item.userNum);
            });
            resolve(result)
        } else {
            reject()
        }
    })

}

// 获取活跃人数
export function getLiveList(params) {
    return new Promise(async (resolve, reject) => {
        const { data: res } = await request.get("/user/activeUser", {
            params
        });
        if (res.code == 200) {
            let result = {
                dayLiveList: [],
                weekLiveList: [],
                monthLiveList: [],
                xlist: [],
                ylist: [],
                name: '活跃人数',
                tipname: '活跃人数',
                url: '/lively',
                responseList: res.result,
                type:'line',
                fn:'getLiveList'
            }
            res.result.forEach(item => {
                result.dayLiveList = res.result.map(item => item.dau);
                result.weekLiveList = res.result.map(item => item.wau);
                result.monthLiveList = res.result.map(item => item.mau);
                result.xlist = res.result.map(item => item.day.slice(5));
                result.ylist = res.result.map(item => item.dau);
            });
            resolve(result)
        } else {
            reject()
        }
    })
}

// 机构销量统计
export function getStatisticData(params) {
    return new Promise(async (resolve, reject) => {
        const { data: res } = await request.get("/agent/statistics", {
            params
        });
        if (res.code == 200) {
            res.result.forEach(item => {
                if (!item.agentName.trim()) {
                    item.agentName = "java";
                }
                item.agentName = item.agentName.slice(0, 4);
                item.amountPercent = item.amountPercent.toFixed(2) + "%";
                item.salePercent = item.salePercent.toFixed(2) + "%";
                item.userPercent = item.userPercent.toFixed(2) + "%";
            });
            let result = {
                userNumList: [],
                saleNumList: [],
                saleAmountList: [],
                xlist: [],
                ylist: [],
                url: '/board/statistics',
                name: '机构销量',
                tipname: '机构销量',
                responseList: res.result,
                type:'hbar',
                fn:'getStatisticData'
            }
            result.saleNumList = res.result.map(
                item => item.saleNum
            );
            result.userNumList = res.result.map(
                item => item.userNum
            );
            result.saleAmountList = res.result.map(
                item => item.saleAmount
            );
            result.xlist = res.result.map(
                item => item.agentName
            )
            result.ylist = result.saleAmountList
            resolve(result)
        } else {
            reject()
        }

    })
}

// 热门学科统计
export function getHotSubjectData(params) {
    return new Promise(async (resolve, reject) => {
        const { data: res } = await request.get("/courseSubject/subjectRank", {
            params
        });
        if (res.code == 200) {
            let result = {
                xlist: [],
                ylist: [],
                url: '/hot',
                responseList: res.result,
                name: '热门学科',
                tipname: '',
                type:'line',
                fn:'getHotSubjectData'
            }
            res.result.forEach(item => {
                result.xlist.push(item.subjectName.slice(0, 4));
                result.ylist.push(item.userNum);
            });
            resolve(result)
        } else {
            reject()
        }
    })
}

// 热门课程TOP5
export function getTop5() {
    return new Promise(async (resolve, reject) => {
        const { data: res } = await request.get("/course/courseUser");
        if (res.code == 200) {
            res.result.forEach(item => {
                item.courseName = item.courseName.slice(0, 4);
                item.weekPercent = item.weekPercent + "%";
            });
            resolve(res.result)
        } else {
            reject()
        }
    })
}
